package com.may.wlrj.config;

import java.util.Date;

import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.ChannelAwareMessageListener;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.may.wlrj.mqtt.MqttHeader;
import com.may.wlrj.mqtt.RegisterResp;
import com.may.wlrj.services.GateWayService;
import com.may.wlrj.util.MQTTUtil;
import com.rabbitmq.client.Channel;

@Configuration
public class Receiver {
	private GateWayService gatewayService;
    @Autowired
    public void setGatewayService(GateWayService gatewayService){
        this.gatewayService = gatewayService;
    }
	private ObjectMapper mapper; 
    private com.fasterxml.jackson.databind.JsonNode rootNode;
	@Bean
    public SimpleMessageListenerContainer messageContainer(ConnectionFactory connectionFactory) {
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
        container.setQueues(new Queue("cloud"));
        container.setExposeListenerChannel(true);
        container.setMaxConcurrentConsumers(4);
        container.setConcurrentConsumers(2);
        // 设置确认模式手工确认
        container.setAcknowledgeMode(AcknowledgeMode.MANUAL);
        container.setMessageListener(new ChannelAwareMessageListener() {
            @Override
            public void onMessage(Message message, Channel channel) throws Exception {
                byte[] body = message.getBody();
                mapper = new ObjectMapper(); 
                
    			rootNode = mapper.readTree(new String(body));
    			try {
    				String reqtype = rootNode.findPath("data").get("attribute").asText();
    				
    				if(reqtype.equalsIgnoreCase("BLE_lock_battery")) {
        				gatewayService.GateWayBattery(rootNode);
        			}else if(reqtype.equalsIgnoreCase("openreport")) {
        				if(!gatewayService.HandleOpenReport(rootNode)) {
        					
        				}
        			}else if(reqtype.equalsIgnoreCase("BLE_lock_fp")) {
        				if(!gatewayService.GateWayParseAuth(rootNode.findPath("data"),rootNode.get("mac").asText())) {
        					
        				}
        			}
        			
    			}catch(Exception e) {
    				e.printStackTrace();
    			}finally {
    				channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
    			}
                 
            }
        });
        return container;
    }	
	
	@Bean
    public SimpleMessageListenerContainer heartbeatmessageContainer(ConnectionFactory connectionFactory) {
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
        container.setQueues(new Queue("hbeat"));
        container.setExposeListenerChannel(true);
        container.setMaxConcurrentConsumers(1);
        container.setConcurrentConsumers(1);
        // 设置确认模式手工确认
        container.setAcknowledgeMode(AcknowledgeMode.MANUAL);
        container.setMessageListener(new ChannelAwareMessageListener() {
            @Override
            public void onMessage(Message message, Channel channel) throws Exception {
                byte[] body = message.getBody();
                mapper = new ObjectMapper(); 
                
    			rootNode = mapper.readTree(new String(body));
    			try {
    				//gatewayService.ParseHeartBeat(rootNode);
        			
        			
    			}catch(Exception e) {
    				e.printStackTrace();
    			}finally {
    				channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
    			}  
            }
        });
        return container;
    }
}
